package org.cangku.controller;

import org.cangku.dao.KuCunDao;
import org.cangku.dao.LskucunDao;
import org.cangku.entity.Kucun;
import org.cangku.entity.Lskucun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/kucun")
public class KuCunController {
    @Autowired
    private KuCunDao kuCunDao;
    @Autowired
    private LskucunDao lskucunDao;

    @RequestMapping("/queryAddkcShop")
    @ResponseBody
    public List<Kucun> queryAddkcShop(int kid){
        return kuCunDao.querykcshop(kid);
    }

    @RequestMapping("/query")
    @ResponseBody
    public Map<String,Object> query(String name,int typeid,int sbrand,int offset,int limit){
        Map<String,Object> map = new HashMap<String, Object>();

        map.put("rows",kuCunDao.queryByTJ(name,typeid,sbrand,offset,limit));
        map.put("total",kuCunDao.getCountByTJ(name,typeid,sbrand));

        return map;
    }
    /*修改商品进价*/
    @RequestMapping("/updatejinjia")
    @ResponseBody
    public Map<String,Object> UpdateJinjia(int hidkid,double jinjia,int kucun){
        Map<String,Object> map = new HashMap<String, Object>();
        double cb = Math.round(jinjia*kucun);
        System.out.println("成本："+cb);
        kuCunDao.updateGoldMoney(hidkid,jinjia,cb);

        map.put("rows",kuCunDao.queryByTJ("",0,0,0,4));
        map.put("total",kuCunDao.getCountByTJ("",0,0));
        return map;
    }

    /*查询库存小于50的商品*/
    @RequestMapping("/queryxiao")
    @ResponseBody
    public List<Kucun> queryByXiao(){
        return kuCunDao.queryXiao();
    }

    /**
     * 二次
     */
    @RequestMapping("/lsadd")
    @ResponseBody
    public Map<String,Object> getInfoBykid(int kid, HttpServletRequest request){
        Map<String,Object> map = new HashMap<String, Object>();

        /*根据kid查询改kid的信息*/
        List<Kucun> kucunList = kuCunDao.getInfoBykId(kid);
        /*总价钱*/
        double money = 0.0;
        Lskucun ll = new Lskucun();
        ll.setDingdan(getdanhao());
        for(Kucun kc:kucunList){
            ll.setSid(kc.getSid());
            ll.setSname(kc.getSname());
            ll.setJinjia(kc.getGoldMoney());
            ll.setPinpai(kc.getSbrand());
            ll.setGhsname(kc.getGhid());
            ll.setSize(kc.getSsize());
            ll.setColor(kc.getScolor());
            ll.setJhnum(1);
            money = kc.getGoldMoney()*1;
            ll.setFenlei(kc.getStype());
            ll.setShoujia(kc.getSaleMoney());
        }
        ll.setZongjine(money);;
        ll.setPdid(1);
        int i = lskucunDao.insert(ll);
        if(i>0){
            map.put("count",1);
            List<Lskucun> lskc = lskucunDao.queryByDanHao(ll.getDingdan());
            String dingdan = "";
            for(Lskucun ls : lskc){
                dingdan = ls.getDingdan();
            }
            double zongjine = lskucunDao.getZongJine(dingdan);
            request.getSession().setAttribute("dingdan",dingdan);
            request.getSession().setAttribute("zongqian",zongjine);
        }
        return map;
    }

    @RequestMapping("/getinfoBykid")
    @ResponseBody
    public Map<String,Object> getinfobykid(int kid,String danhao,int num){
        Map<String,Object> map = new HashMap<String, Object>();
        Lskucun ls = new Lskucun();
        List<Kucun> kclist = kuCunDao.getInfoBykId(kid);
        //订单号
        ls.setDingdan(danhao);
        ls.setJhnum(num);
        double money = 0.0; //销售总金额
        for (Kucun kc:kclist){
            ls.setSid(kc.getSid());
            ls.setSname(kc.getSname());
            ls.setJinjia(kc.getGoldMoney());
            ls.setPinpai(kc.getSbrand());
            ls.setGhsname(kc.getGhid());
            money = kc.getGoldMoney() * num;
            ls.setZongjine(money);
            ls.setSize(kc.getSsize());
            ls.setColor(kc.getScolor());
            ls.setFenlei(kc.getStype());
            ls.setShoujia(kc.getSaleMoney());
            ls.setPdid(0);
        }
        int i = lskucunDao.extendsCunzai(ls);
        if(i>0){
            int u = lskucunDao.updateNumAndMoney(ls);
            map.put("row",1);
            map.put("ling",lskucunDao.queryInfoByddandpdid(danhao));
        }else{
            int row = lskucunDao.insert(ls);
            map.put("row",1);
            map.put("ling",lskucunDao.queryInfoByddandpdid(danhao));
        }
        return map;
    }

    @RequestMapping("/getdingdan")
    @ResponseBody
    public Map<String,Object> getdingdan(){
        Map<String,Object> map = new HashMap<String,Object>();
        String dd = getdanhao();
        map.put("dingdan",dd);
        return map;
    }

    public static String getdanhao(){
        /*生成订单号*/
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        return newDate+result;
    }
}
